package cn.ekuma.epos.catalog;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.sourceforge.jtimepiece.TimeUtil;

import cn.ekuma.epos.datalogic.I_DataLogicSales;
import cn.ekuma.epos.db.table.I_Category;
import cn.ekuma.epos.db.table.I_ProductExt;

import com.openbravo.bean.Product;
import com.openbravo.bean.ProductCategory;
import com.openbravo.bean.Tax;
import com.openbravo.bean.sales.ShoppingCart;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.query.QBFParameter;
import com.openbravo.data.loader.query.QBFParameters;
import com.openbravo.pos.bean.ProductCategoryExt;
import com.openbravo.pos.bean.ProductExt;
import com.openbravo.pos.sales.TaxesLogic;

public class CatelogManager {
    private int picWidth,picHeight;
	private boolean pricevisible;
	private boolean taxesincluded;
	private I_DataLogicSales  m_dlSales;
	private List<ProductExt> products;
	private TaxesLogic taxeslogic;
	private ShoppingCart cart;
	
	
	
	public CatelogManager(I_DataLogicSales dlSales, boolean pricevisible, boolean taxesincluded,int picWidth, int picHeight) {
		this.picWidth = picWidth;
		this.picHeight = picHeight;
		this.pricevisible = pricevisible;
		this.taxesincluded = taxesincluded;
		this.m_dlSales = dlSales;
		cart=new ShoppingCart();
	}

	public CatelogManager(I_DataLogicSales dlSales) {
		this(dlSales,true,true,400,300);
	}

	public List<ProductExt> getProducts() {
		if(products==null)
			return new ArrayList();
		return products;
	}

	public int getPicWidth() {
		
		return picWidth;
	}

	public int getPicHeight() {
		
		return picHeight;
	}

	public boolean isPricevisible() {
	
		return pricevisible;
	}

	public boolean isTaxesincluded() {
		return taxesincluded;
	}

	public Tax getTaxInfo(String taxCategoryID) {
		return  taxeslogic.getTaxInfo(taxCategoryID);
	}

	public void queryByCatId(String catid,boolean allProduct) throws BasicException {
		if(taxeslogic==null);
			 taxeslogic = new TaxesLogic(m_dlSales.list(Tax.class));
		 if(!allProduct)
		    products= m_dlSales.getProductCatalog(catid);
		 else
			 products=m_dlSales.list(ProductExt.class, ProductCategory.class, catid);
	}


	public void getPrefPriceProductExt()throws BasicException {
		products=m_dlSales.getPrefPriceProductExt();
	}
	
	public ShoppingCart getCart() {
		return cart;
	}

	public void getNewProductExt()throws BasicException{
		QBFParameters paras=new QBFParameters();
		paras.and(new QBFParameter(I_ProductExt.ISCATALOG).equal(true));
		Date time=new Date();
		paras.and(new QBFParameter(I_ProductExt.LASTMODIFIED).less(time));
		paras.and(new QBFParameter(I_ProductExt.LASTMODIFIED).greaterOrEqual(TimeUtil.addDay(time, -6)));
		products= m_dlSales.query(ProductExt.class, paras);
	}
	
	public void searchProduct(String sCode)throws BasicException{
		QBFParameters paras=new QBFParameters();
		if (sCode.startsWith("~")||sCode.startsWith("～")) {
			sCode=sCode.substring(1);
			sCode.replace(" ", "%");
			paras.and(new QBFParameter(I_ProductExt.NAME).link(sCode));
			paras.or(new QBFParameter(I_ProductExt.MNEMONIC).link(sCode));
		} else {
			paras.and(new QBFParameter(I_ProductExt.CODE).equal(sCode));
			paras.or(new QBFParameter(I_ProductExt.REFERENCE).equal(sCode));
		}
		products= m_dlSales.query(ProductExt.class, paras);
	}
	
	public void addShoppingProduct(Product obj,double d){
		cart.addShoppingProduct(obj, d);
	}

	public List<ProductCategoryExt> getHotCategories() throws BasicException {
		return m_dlSales.queryByQBFParameter(ProductCategoryExt.class, new QBFParameter(I_Category.ISHOT).equal(true));
	}

	public List<ProductExt> getRecommendProducts(String productId) throws BasicException{
		return m_dlSales.getRecommendProducts(productId);
	}
}
